package Youtube.authentication;

import java.io.File;
import java.util.List;

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;

public class Authentication {

	/** Global instance of the HTTP transport. */
	public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

	/** Global instance of the JSON factory. */
	public static final JsonFactory JSON_FACTORY = new JacksonFactory();


	private static String CLIENT_ID = "364180220849-u0lpbbp734dhdfd3ibq8quu6vb4si7bg.apps.googleusercontent.com";
	private static String CLIENT_SECRET = "PpiemuziXgm9o4YirV4YJpge";
	private static AuthorizationCodeFlow flow;

	/**
	 * Authorizes the installed application to access user's protected data.
	 *
	 * @param scopes list of scopes needed to run upload.
	 */

	public Credential authorize(List<String> scopes) throws Exception {
				
		// Set up file credential store.
		FileDataStoreFactory credentialStore = new FileDataStoreFactory(new File(System.getProperty("user.home"), ".credentials/youtube-api.json"));

		// Set up authorization code flow.
		flow = new GoogleAuthorizationCodeFlow.Builder(
				HTTP_TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, scopes).setDataStoreFactory(credentialStore).setAccessType("online")
				.setApprovalPrompt("force").build();

		LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();   	
		flow.newAuthorizationUrl().setState("xyz").build();

		return new AuthorizationCodeInstalledApp(flow, localReceiver ).authorize("user");
	}

}
